<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('申请标题')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('姓名')" prop="user.userId">
            <UserSelect
              ref="userSelect"
              v-model="form.user.userId"
              :placeholder="$t('姓名')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('外派职位')" prop="level">
            <el-select v-if="levelResoure === 'bf'" v-model="form.level" filterable :placeholder="$t('外派职位')">
              <el-option
                v-for="dict in appointOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
            <el-select v-else v-model="form.level" filterable :placeholder="$t('外派职位')">
              <el-option
                v-for="dict in levelOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('外派时间')" prop="expatsTime">
            <DatePicker v-model="form.expatsTime" :placeholder="$t('外派时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否结束')">
            <el-radio-group v-model="form.finish" @change="finishOptions">
              <el-radio
                v-for="dict in yesNoOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('外派地点')" prop="expatsAddress">
            <el-input v-model="form.expatsAddress" maxlength="125" show-word-limit :placeholder="$t('外派地点')" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.finish=='Y' " :span="12">
          <el-form-item :label="$t('外派结束时间')" prop="expatsEndTime">
            <DatePicker v-model="form.expatsEndTime" :placeholder="$t('外派结束时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('说明')" prop="explain">
            <el-input
              v-model="form.explain"
              type="textarea"
              :rows="2"
              :placeholder="$t('说明')"
              show-word-limit
              maxlength="2000"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable
        :list.sync="form.fileList"
        data-type="deal_expats"
        has-permi="deal:expats"
        :business-id="autoBusinessId($route.query.id, form)"
        :btn-show="!otherBtnHide"
      />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="deal:expats" :menu-key="menuKey" :btn-hide="btnHide" />
    </div>
  </div>
</template>
<script>
import { saveDealExpats, getDealExpats } from '@/api/deal/expats/dealExpats'
import { getDealName, getDealType } from '@/api/base/deal/dealBase'

export default {
  data() {
    return {
      // 菜单标识
      menuKey: 'deal_expats',
      // 是否隐藏保存按钮
      btnHide: false,
      // 是否隐藏其他按钮
      otherBtnHide: false,
      // 级别 字典deal_level_type字典 债权投资使用
      levelOptions: [],
      appointOptions: [], // 参股基金
      // 是否结束 y/n 按钮
      yesNoOptions: [],
      // 外派人员来源
      levelResoure: '',
      // // 结束时间默认关闭
      // isShowEndTime: false,
      form: {
        fileList: [],
        user: {
          userId: undefined
        },
        expatsEndTime: undefined
      },

      // 表单校验
      rules: {
        'user.userId': [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        ], level: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        ], expatsTime: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        ], actTitle: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], expatsEndTime: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        ]
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true
    }
  },
  created() {
    // 级别 字典deal_level_type字典 债权投资使用
    this.getDicts('deal_level_type').then(response => {
      this.levelOptions = response.data
    })
    // 级别 字典deal_level_type字典 参股基金使用
    this.getDicts('appoint_position').then(response => {
      this.appointOptions = response.data
    })
    // 是否结束 字典sys_yes_no字典翻译
    this.getDicts('sys_yes_no').then(response => {
      this.yesNoOptions = response.data
    })
    this.$bus.$emit('async_done')
  },
  methods: {
    finishOptions(value) {
      if (value === 'Y') {
        // this.isShowEndTime = true
      } else {
        // this.isShowEndTime = false
        this.form.expatsEndTime = undefined
      }
    },
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealExpats(data.id).then(response => {
          this.form = response.data
          this.getDealType(this.form.dealId)
          this.loading = false
          this.finishOptions(this.form.finish)
        })
      } else {
        this.reset()
        this.getDealType(this.$route.query.id)
        this.loading = false
      }
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        user: {
          userId: undefined
        },
        dealId: this.$route.query.id,
        companyId: this.$route.query.companyId,
        level: undefined,
        expatsTime: undefined,
        expatsAddress: undefined,
        explain: undefined,
        createBy: undefined,
        createTime: undefined,
        fileList: [],
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        actId: undefined,
        actStatus: undefined,
        expatsEndTime: undefined,
        finish: 'N'
      }
      this.resetForm('form')
    },
    // 取消按钮
    getDealType(dealId) {
      // 获取所有上市项目
      getDealType(dealId).then(response => {
        this.levelResoure = response.data
        console.log(response.data)
      })
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },

    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveDealExpats(this.form).then(response => {
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  // 启动流程
                  this.form.id = response.data.id
                  this.startProcess(response.data.id)
                  // 点击的仅保存
                } else {
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm()
                }
                resolve()
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject()
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: async function(dataId) {
      var dealName = await getDealName(this.$route.query.id)
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = '「' + dealName + '」' + ':' + this.form.actTitle
      // 业务表名
      processData.dataTable = 'deal_expats'
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/deal/expats/dealExpatsForm'
      processData.viewPath = '@/views/deal/expats/dealExpatsView'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.menuKey
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }

  }

}
</script>
